<?php

class Forget extends MY_Controller {
	function __construct(){
		parent::__construct();
	}

	public function index(){
		$this->smarty->display("forget.html");
	}

	function check_vcode(){
		$post_data = $this->input->post();
		if(empty($post_data['phone'])){
			$result['error'] = 1;
            $result['msg'] = '缺少手机号码';
		}else{
			$this->load->model("Verification_code");
	        $code_model = $this->Verification_code->getLastCodeByPhone($post_data['phone'],3);
	        if($code_model['is_use'] == 1 || $code_model['code'] != $post_data['vcode']){
	            //验证码错误
	            $result['error'] = 1;
	            $result['msg'] = '验证码错误';
	        }else{
	        	$result['error'] = 0;
	            $result['msg'] = '';
	        }
		}
		echo json_encode($result);
	}

	function send_msg($phone){
	 	$this->load->model("Verification_code");
        $code_model = $this->Verification_code->getLastCodeByPhone($phone, 3);
        $user_info = get_field_by_where("user", "id", "mobile='{$phone}' AND school_id = '{$this->school_id}'");
        if(empty($user_info)){
        	print_r('{"code":500,"msg":"手机号码尚未注册"}');
            return ;
        }
        if(empty($code_model) || strtotime($code_model['create_on'])+1*600<time()){
            $code = getRandChar(4);
            $result = $this->Verification_code->insertCodeLog($phone,$code,3);
            if($result>0){
                $send_result = send_verify_code_msg($phone,$code);
                print_r($send_result);exit;
                /*$send_result = json_decode($send_result, true);
                if(json_decode($send_result)->code==200){
                    print_r('{"code":201,"msg":"已注册、未开店"}');exit;
                }else{
                    print_r($send_result);exit;
                }
                return ;*/
            }else{
                print_r('{"code":400,"msg":"系统异常"}');
                return ;
            }
        }else{
            print_r('{"code":500,"msg":"您操作太频率了！请稍后"}');
            return ;
        }
	}

	function change_password(){
		$post_data = $this->input->post();
		if(empty($post_data['phone'])){
			$result['error'] = 1;
            $result['msg'] = '缺少手机号码';
            echo json_encode($result);exit;
        }
        if(empty($post_data['vcode'])){
			$result['error'] = 1;
            $result['msg'] = '缺少验证码';
            echo json_encode($result);exit;
        }
        if(empty($post_data['password'])){
			$result['error'] = 1;
            $result['msg'] = '缺少新密码';
            echo json_encode($result);exit;
        }
        if(empty($post_data['con_password'])){
			$result['error'] = 1;
            $result['msg'] = '缺少确认密码';
            echo json_encode($result);exit;
        }
        if($post_data['con_password'] != $post_data['password']){
            $result['error'] = 1;
            $result['msg'] = '两次密码输入不一致';
            echo json_encode($result);exit;
        }
        $this->load->model("Verification_code");
        $code_model = $this->Verification_code->getLastCodeByPhone($post_data['phone'], 3);
        if($code_model['is_use'] == 1 || $code_model['code'] != $post_data['vcode']){
            //验证码错误
            $result['error'] = 1;
            $result['msg'] = '验证码错误';
            echo json_encode($result);exit;
        }
        //修改密码操作
        $rand_str = create_rand_str(10, 10);
        $new_password = md5(md5($post_data['password']) . $rand_str);
		$this->db->where('mobile', $post_data['phone']);
		$data['rand_str'] = $rand_str;
		$data['password'] = $new_password;
		$results = $this->db->update('user', $data);
		show_message('/login', "密码修改成功，请登录");exit;
	}
}
